//
//  AppDelegate.m
//  SMealUserSide
//
//  Created by apple on 2018/2/5.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "AppDelegate.h"
#import "AppDelegate+ZBAddNetwork.h"
#import "AppDelegate+ZBLogicService.h"
#import "AppDelegate+UIViewController.h"
#import "ZB_AppDataManger.h"
#import "ZB_LoginInterfaceDM.h"
#import <ZBDebuggerTool.h>

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [[ZB_AppDataManger shareInstance] zb_application:application didFinishLaunchingWithOptions:launchOptions];
    // 进入app逻辑
    [self loadRootViewController];
    
    //增加观察者
    [self addAppObsevers];
    
    //增加App监视器
    [[ZBDebuggerTool shareDebuggerTool] startWorking];
   
    
    
    
    return YES;
}



#pragma mark - 登录事件
- (void)addAppObsevers
{
    //登录成功
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didLoginSuccess) name:ZB_LOGIN_SUCCESS_NOTIFATIONNAME object:nil];
    //未登录
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willLoginEvent) name:ZB_LOGIN_NOTLOGIN_NOTIFATIONNAME object:nil];
}

//未登录 跳转到登录
- (void)willLoginEvent
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        ZB_NavigationViewController *loginVC =  [ZB_LoginInterfaceDM l_instaceNavcLoginViewController];
        UIViewController *currentVC = [self getCurrentViewController];
        [currentVC presentViewController:loginVC animated:YES completion:nil];
    });
    
}

//登录成功
- (void)didLoginSuccess
{
    //检测版本更新
    [self loadAppVersion];

}
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(nonnull NSData *)deviceToken{
    [[ZB_AppDataManger shareInstance] zb_application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
    [[ZB_AppDataManger shareInstance] zb_application:application didFailToRegisterForRemoteNotificationsWithError:error];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
    [[ZB_AppDataManger shareInstance] zb_application:application didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
    [[ZB_AppDataManger shareInstance] zb_application:application didReceiveRemoteNotification:userInfo];
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
    [[ZB_AppDataManger shareInstance] zb_applicationDidEnterBackground:application];
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    [[ZB_AppDataManger shareInstance] zb_applicationWillEnterForeground:application];
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    //角标清0
    if ([UIApplication sharedApplication].applicationIconBadgeNumber) {
        [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
    }
}







@end
